package org.urchr.beorganized.mobile.base.page;

import org.apache.wicket.PageParameters;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.behavior.HeaderContributor;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.urchr.beorganized.mobile.base.Application;

public class BasePage extends WebPage {

	public BasePage(PageParameters parameters) {
		super(parameters);
		add(HeaderContributor.forCss(
				new ResourceReference(Application.class, "css/mobile.css")));
		add(new Label("pageTitle", "Mobile BeOrganized!"));
		add(new Label("error_place", ""));
	}
	
	public void resetError(){
		addOrReplace(new Label("error_place", ""));
	}
	
	public void showError(String error){
		System.out.println("Error: "+error.replaceAll("\\'", "\\'"));
		addOrReplace(new Label("error_place", "alert('Error: "+error.replaceAll("\\'", "\\'")+"');").setEscapeModelStrings(false));
	}
	
	protected int safeInt(String s, int d){
		if(s==null || s.equals(""))
			return d;
		try {
			return Integer.parseInt(s);
		} catch (Exception e) {
		}
		return d;
	}
	
	protected String escapeHTML(String data){
		if(data==null)
			return "";
		data = data.replaceAll("\\&", "&amp;");
		data = data.replaceAll("\\<", "&lt;");
		data = data.replaceAll("\\>", "&gt;");
		data = data.replaceAll("\\n", "<br/>");
		return data;
	}
}
